package com.icinfo.cloud.provider.peripheralinterface.zszf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.peripheralinterface.zszf.dto.HandLawWaitApprovalQueryDto;
import com.icinfo.cloud.provider.peripheralinterface.zszf.dto.HandLawWaitApprovalRecordPageQueryDto;
import com.icinfo.cloud.provider.peripheralinterface.zszf.service.IHandLawIInterfaceService;
import com.icinfo.cloud.provider.peripheralinterface.zszf.vo.WaitApprovalBasicInfoVo;
import com.icinfo.cloud.provider.punish.approval.dto.AuditQueryPageDto;
import com.icinfo.cloud.provider.punish.approval.model.CaseApprovalWaitRecord;
import com.icinfo.cloud.provider.punish.approval.vo.AuditInfoListVo;
import com.icinfo.cloud.provider.punish.mapper.CaseApprovalWaitRecordMapper;
import com.icinfo.cloud.provider.punish.service.ICaseApprovalWaitRecordService;
import com.icinfo.cloud.provider.punish.ucase.urge.dto.CaseUrgeAddDto;
import com.icinfo.cloud.provider.punish.ucase.urge.dto.CaseUrgeDetailDto;
import com.icinfo.cloud.provider.punish.ucase.urge.dto.CaseUrgePageRequest;
import com.icinfo.cloud.provider.punish.ucase.urge.dto.CaseUrgePageResponse;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.cloud.provider.common.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 * 描述：掌上执法接口服务实现类
 * @author shishengyao
 * @description
 * @date 2023/4/18
 */
@Service
@Slf4j
public class HandLawIInterfaceServiceImpl implements IHandLawIInterfaceService {
    @Value("${tzService.url}")
    private String url;
    @Resource
    private ICaseApprovalWaitRecordService caseApprovalWaitRecordService;
    @Resource
    private CaseApprovalWaitRecordMapper caseApprovalWaitRecordMapper;
    @Override
    public WaitApprovalBasicInfoVo getWaitApprovalBasicInfoVo(HandLawWaitApprovalQueryDto queryDto) {
        WaitApprovalBasicInfoVo vo=new WaitApprovalBasicInfoVo();
        String userId=queryDto.getUserId();
        String orgCode=queryDto.getOrgCode();
        //获取待审核案件数量
        int waitApprovalCount = caseApprovalWaitRecordService.countWaitApproval(userId,orgCode);
        vo.setWaitApprovalCount(waitApprovalCount);
        CaseApprovalWaitRecord caseApprovalWaitRecord = caseApprovalWaitRecordMapper.selectEarliestApprovalRecordByUser(userId, orgCode);
        if (ObjectUtil.isNotEmpty(caseApprovalWaitRecord)){
            vo.setApprovalLimitDate(caseApprovalWaitRecord.getApprovalCurrentPlanendTime());
        }
        return vo;
    }

    @Override
    public Result<PageResponse<AuditInfoListVo>> getWaitApprovalRecordList(HandLawWaitApprovalRecordPageQueryDto queryDto) {
        UserDTO userDTO = UserUtil.getUserInfo();
        if (userDTO == null) {
            return Result.success(450, "token无效");
        }
        Map<String, String> headers = HeaderUtil.headerInfo();
        AuditQueryPageDto auditQueryPageDto=new AuditQueryPageDto();
        //移动端排除修改文书的审批记录
        auditQueryPageDto.setLength(queryDto.getLength());
        auditQueryPageDto.setPageNum(queryDto.getPageNum());
        auditQueryPageDto.setApprovalTitle(queryDto.getApprovalTitle());
        auditQueryPageDto.setOrgCode(queryDto.getOrgCode());
        auditQueryPageDto.setUserId(queryDto.getUserId());
        if (Constant.STRING_0.equals(queryDto.getApprovalStatus())){
            auditQueryPageDto.setIsQueryEditRecord("0");
            String param = JSON.toJSONString(auditQueryPageDto);
            String response = HttpUtil.createPost(url.concat("waitAudit/getWaitAuditList")).addHeaders(headers).body(param).execute().body();
            Result<PageResponse<AuditInfoListVo>> result = JSON.parseObject(response, Result.class);
            return result;
        }
        if (Constant.STRING_1.equals(queryDto.getApprovalStatus())){
            String param = JSON.toJSONString(auditQueryPageDto);
            String response = HttpUtil.createPost(url.concat("audit/getOffAuditList")).addHeaders(headers).body(param).execute().body();
            Result<PageResponse<AuditInfoListVo>> result = JSON.parseObject(response, Result.class);
            return result;
        }
        return null;
    }

    @Override
    public Result<PageResponse<CaseUrgePageResponse>> getCaseUrgePageList(CaseUrgePageRequest pageRequest) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String param = JSON.toJSONString(pageRequest);
        String response = HttpUtil.createPost(url.concat("punish/ucase/caseUrge/caseUrgePage")).addHeaders(headers).body(param).execute().body();
        Result<PageResponse<CaseUrgePageResponse>> result = JSON.parseObject(response, Result.class);
        return result;
    }

    @Override
    public Result<List<CaseUrgeDetailDto>>  getUrgeDetailList(String caseId) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String response = HttpUtil.createGet(url.concat("punish/ucase/caseUrge/caseUrgeDetail?caseId="+caseId)).addHeaders(headers).execute().body();
        Result<List<CaseUrgeDetailDto>> result = JSON.parseObject(response, Result.class);
        return result;
    }

    @Override
    public Result<String> saveCaseUrge(CaseUrgeAddDto dto) {
        Map<String, String> headers = HeaderUtil.headerInfo();
        String param = JSON.toJSONString(dto);
        String response = HttpUtil.createPost(url.concat("punish/ucase/caseUrge/addCaseUrge")).addHeaders(headers).body(param).execute().body();
        Result<String> result = JSON.parseObject(response, Result.class);
        return result;
    }

}
